﻿using AppSocketModel;
using BCGPSCommon;
using BCGPSMongoData;

namespace AppSocketControl.GpsData.DataDispatch
{
    public class DispatchSleepTimeOnErrCmdReturn
    {
        private readonly IRedisService redisService;
        private readonly MongoDBCommand<SleepTimeOnErrDto> mongoDBCommand;
        private SleepTimeOnErrDto sleepTimeOnErr = new SleepTimeOnErrDto();
        public DispatchSleepTimeOnErrCmdReturn()
        {
            redisService = new RedisService();
            mongoDBCommand = new MongoDBCommand<SleepTimeOnErrDto>("CmdSleepTimeOnErrHistory");
        }

        private void Split(string[] dataArr)
        {
            sleepTimeOnErr.IMEI = dataArr[5];

            sleepTimeOnErr.CmdDateTime = MongoDBDateTime.GetNowUtc();
            sleepTimeOnErr.CmdToward = "Receive";
            sleepTimeOnErr.CmdCode = ((int)GpsCmdCode.SleepTimeOnErr).ToString();
            sleepTimeOnErr.Result = dataArr[7];

        }
        /// <summary>
        /// 设备运行状态数据写入数据库
        /// </summary>
        /// <param name="dataArr"></param>
        public void Run(string[] dataArr)
        { 
            Split(dataArr);
            Msg.WirteLine(sleepTimeOnErr.Result, 10, '-');
            mongoDBCommand.InsertDocumentAsync(sleepTimeOnErr).GetAwaiter().GetResult();
            redisService.AddAsync(GeneralContainer.RedisNameSpaceIMEISendCmd + sleepTimeOnErr.IMEI, string.Empty).GetAwaiter().GetResult();
        }
    }
}
